package com.gopay.solution.dao.impl;

import com.gopay.solution.domain.po.SsTransSettleDtl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 方案服务 交易结算方案明细DAO
 * 
 * @SsTransSettleDtlDAOImpl.java
 * @author wanggang
 * @2015-3-20 上午11:24:54 www.gopay.com.cn Inc.All rights reserved.
 */
@SuppressWarnings({ "unchecked", "rawtypes" })
@Repository("ssTransSettleDtlDAO")
public class SsTransSettleDtlDAOImpl extends
		SolutionRuleDtlDAOImpl<SsTransSettleDtl, Serializable> {
	@Override
	public SsTransSettleDtl getByPk(Serializable id) {
		return find(SsTransSettleDtl.class, id);
	}

	@Override
	public List<SsTransSettleDtl> listBySolutionId(long solutionId) {
		String hql = "from SsTransSettleDtl where solutionId = " + solutionId;
		return findByHql(hql);
	}

	@Override
	public boolean delBySolutionId(long solutionId) {

		List<SsTransSettleDtl> dtls = listBySolutionId(solutionId);
		if (dtls != null && dtls.size() > 0) {
			deleteAll(dtls);
		}
		return true;
	}

	public List<SsTransSettleDtl> listBySoluIdAndIntTxnCd(long solutionId,
			String intTxnCd) {
		String hql = "from SsTransSettleDtl dtl where dtl.solutionId = :SolutionId and dtl.gopayIntTxnCd = :GopayIntTxnCd";
		Map params = new HashMap();
		params.put("SolutionId", solutionId);
		params.put("GopayIntTxnCd", intTxnCd);
		if(StringUtils.contains(intTxnCd,",")){
			hql = "from SsTransSettleDtl dtl where dtl.solutionId = :SolutionId and dtl.gopayIntTxnCd in ( :GopayIntTxnCd)";
			params.put("GopayIntTxnCd", intTxnCd.split(","));
		}
		return findByHql(hql, params);
	}

	@Override
	public boolean delBySoluIdAndIntTxnCd(long solutionId, String intTxnCd) {
		List<SsTransSettleDtl> dtls = listBySoluIdAndIntTxnCd(solutionId,
				intTxnCd);
		if (dtls != null && dtls.size() > 0) {
			deleteAll(dtls);
		}
		return true;
	}
}
